package cn.bnjy.core;

import cn.bnjy.util.DateUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.util.Date;

/**
 * 描述：
 *
 * @date: 2018/11/3 13:44
 * @author: chavin
 */
@Setter
@Getter
@ToString
@AllArgsConstructor
public class Result implements Serializable {

    private boolean success;    // 状态
    private int code;           // 状态码
    private String message;     // 描述
    private Object data;        // 数据
    private Date date;          // 时间

    public String getDate() {
        return DateUtils.getDateStr(date, null);
    }

    private Result() {

    }

    public static Result success() {
        return success(null);
    }

    public static Result success(String message) {
        return success(message, null);
    }

    public static Result success(Object data) {
        return success(StatusEnum.SUCCESS.getCode(), StatusEnum.SUCCESS.getMessage(), data);
    }

    public static Result success(String message, Object data) {
        return success(StatusEnum.SUCCESS.getCode(), message, data);
    }

    public static Result success(int code, String message, Object data) {
        return success(true, code, message, data);
    }

    public static Result success(boolean success, int code, String message, Object data) {
        return new Result(success, code, message, data, new Date());
    }

    public static Result error() {
        return error(null);
    }

    public static Result error(String message) {
        return error(message, null);
    }

    public static Result error(Object data) {
        return error(false, StatusEnum.ERROR.getCode(), StatusEnum.ERROR.getMessage(), data);
    }

    public static Result error(String message, Object data) {
        return error(false, StatusEnum.ERROR.getCode(), message, data);
    }

    public static Result error(int code, String message, Object data) {
        return error(false, code, message, data);
    }

    public static Result error(boolean success, int code, String message, Object data) {
        return new Result(success, code, message, data, new Date());
    }

}
